using Microsoft.AspNetCore.SignalR;
using MesDashboardApi.Api.Models;
using MesDashboardApi.Api.Services;
using System;
using System.Threading.Tasks;

namespace MesDashboardApi.Api.Hubs
{
    public class DashboardHub : Hub
    {
        private readonly DashboardDataService _dataService;

        public DashboardHub(DashboardDataService dataService)
        {
            _dataService = dataService;
        }

        // 客户端连接成功后调用此方法获取初始数据
        public async Task GetInitialData()
        {
            var data = _dataService.GetDashboardData();
            await Clients.Caller.SendAsync("ReceiveInitialData", data);
        }

        // 更新生产计划数据
        public async Task UpdatePlanData()
        {
            var planData = _dataService.UpdatePlanData();
            await Clients.All.SendAsync("ReceivePlanData", planData);
        }

        // 更新设备运行状态数据
        public async Task UpdateDeviceData()
        {
            var deviceData = _dataService.UpdateDeviceData();
            await Clients.All.SendAsync("ReceiveDeviceData", deviceData);
        }

        // 更新生产概况数据
        public async Task UpdateProductionConfig()
        {
            var productionConfig = _dataService.UpdateProductionConfig();
            await Clients.All.SendAsync("ReceiveProductionConfig", productionConfig);
        }

        // 更新生产线状态
        public async Task UpdateProductionStations()
        {
            var stations = _dataService.UpdateProductionStations();
            await Clients.All.SendAsync("ReceiveProductionStations", stations);
        }

        // 更新品质监控数据
        public async Task UpdateQualityData()
        {
            var qualityData = _dataService.UpdateQualityData();
            await Clients.All.SendAsync("ReceiveQualityData", qualityData);
        }

        // 更新告警信息
        public async Task UpdateAlarmList()
        {
            var alarmList = _dataService.UpdateAlarmList();
            await Clients.All.SendAsync("ReceiveAlarmList", alarmList);
        }

        // 更新所有数据
        public async Task UpdateAllData()
        {
            var data = _dataService.GetDashboardData();
            await Clients.All.SendAsync("ReceiveAllData", data);
        }

        // 当前时间更新
        public async Task UpdateCurrentTime()
        {
            var time = _dataService.GetCurrentTime();
            await Clients.All.SendAsync("ReceiveCurrentTime", time);
        }

        // 客户端连接时
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
            await Clients.Caller.SendAsync("ConnectionEstablished", "连接成功");
        }
    }
} 